package csu.web.mypetstore.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.mapper.ProfileMapper;
import csu.web.mypetstore.mapper.SignonMapper;
import csu.web.mypetstore.pojo.Account;
import csu.web.mypetstore.pojo.Profile;
import csu.web.mypetstore.pojo.Signon;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 18632
 * @description 针对表【account】的数据库操作Service实现
 * @createDate 2025-03-10 16:26:19
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
        implements AccountService{
    @Autowired
    private SignonMapper signonMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ProfileMapper profileMapper;

    @Override
    public Account getAccount(String username, String password) {

        Wrapper<Signon>wrapper=new LambdaQueryWrapper<Signon>()
                .eq(Signon::getUsername,username);

        Signon signon=signonMapper.selectOne(wrapper);
        if(signon!=null&&signon.getPassword().equals(password))
        {
            Wrapper<Account>wrapper2=new LambdaQueryWrapper<Account>()
                    .eq(Account::getUserid,username);

            Account account=accountMapper.selectOne(wrapper2);
            account.setPassword(signon.getPassword());
            account.setUsername(signon.getUsername());
            account.setImage(signon.getImage());
//                    =new Account();
//            account.setPassword(signon.getPassword());
//            account.setUsername(signon.getUsername());
//            account.setImage(signon.getImage());

            return account;
        }
        else {
            return null;
        }


    }

    @Override
    public Account getAccount(String username) {
        Wrapper<Signon>wrapper=new LambdaQueryWrapper<Signon>()
                .eq(Signon::getUsername,username);
        Signon signon=signonMapper.selectOne(wrapper);
        if(signon==null) {
            return null;
        }
        Wrapper<Account>wrapper2=new LambdaQueryWrapper<Account>()
                .eq(Account::getUserid,username);
        Account account=new Account();
        account.setPassword(signon.getPassword());
        account.setUsername(signon.getUsername());

        return account;

    }

    @Override
    public void insertAccount(String username, String password, String email, String firstName, String lastName, String status, String address1, String address2, String city, String state, Integer zip, String country, String phone) {
        Account account=new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setUserid(username);
        account.setEmail(email);
        account.setFirstname(firstName);
        account.setLastname(lastName);
        account.setStatus(status);
        account.setAddr1(address1);
        account.setAddr2(address2);
        account.setCity(city);
        account.setState(state);
        account.setZip(zip);
        account.setPhone(phone);
        account.setCountry(country);
        accountMapper.insert(account);
    }

    @Override
    public void insertSignon(String username, String password,String image) {
        Signon signon=new Signon();
        signon.setUsername(username);
        signon.setPassword(password);
        signon.setImage(image);

        signonMapper.insert(signon);

    }
    @Override
    public void updateSignon(String username, String password) {
        // 创建查询条件，查找用户名对应的Signon对象
        Wrapper<Signon> wrapper = new LambdaQueryWrapper<Signon>()
                .eq(Signon::getUsername, username);

        // 从数据库中获取Signon对象
        Signon signon = signonMapper.selectOne(wrapper);

        // 检查是否找到了对应的Signon对象
        if (signon != null) {
            // 设置新的密码
            signon.setPassword(password);

            // 使用update方法更新符合条件的记录
            signonMapper.update(signon, wrapper);
        } else {
            // 如果没有找到用户，抛出异常
            throw new RuntimeException("找不到用户名为 " + username + " 的用户");
        }
    }

    @Override
    public void updateAccount(String email, String firstname, String lastname, String addr1, String addr2, String city, String state, String zip, String country, String phone, String username) {

    }


    @Override
    public void deleteAccount(String username) {
        Wrapper<Account>wrapper=new LambdaQueryWrapper<Account>()
                .eq(Account::getUserid,username);
        accountMapper.delete(wrapper);
        Wrapper<Signon>wrapper2=new LambdaQueryWrapper<Signon>()
                .eq(Signon::getUsername,username);
        signonMapper.delete(wrapper2);
    }
    @Override
    public void insertProfile(String username, String langpref, String favcategory) {

        Profile profile=new Profile();
        profile.setUserid(username);
        profile.setLangpref(langpref);
        profile.setFavcategory(favcategory);
        profile.setBanneropt(1);
        profile.setMylistopt(1);
        profileMapper.insert(profile);



    }



    @Override
    public void updateAccount(Account account) {
        updateSignon(account.getUsername(),account.getPassword());
        LambdaUpdateWrapper<Account> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Account::getUserid, account.getUsername());
        accountMapper.update(account, updateWrapper);
    }
}




